package com.medical.smartcare.activity;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.medical.smartcare.R;
import com.medical.smartcare.asynctask.WebApiAsyncTask;

public class ManageActivity extends Activity
{
	private static final String TAG = ManageActivity.class.getSimpleName();

	private TextView tvName, tvEmail, tvApiKey, tvCreateDate, tvTasks, tvNric, tvMobilePhone, tvHomePhone, tvType, tvAuthorityLevel;
	private Button bntReload;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		try
		{
			super.onCreate(savedInstanceState);

			Bundle bundle = getIntent().getExtras();

			if (bundle == null)
			{
				Log.e(TAG, "bundle is null!");
			}
			else
			{
				setContentView(R.layout.activity_manage);

				tvName = (TextView) findViewById(R.id.tvName);
				tvEmail = (TextView) findViewById(R.id.tvEmail);
				tvApiKey = (TextView) findViewById(R.id.tvApiKey);
				tvCreateDate = (TextView) findViewById(R.id.tvCreateDate);
				tvTasks = (TextView) findViewById(R.id.tvTasks);
				tvNric = (TextView) findViewById(R.id.tvNric);
				tvMobilePhone = (TextView) findViewById(R.id.tvMobilePhone);
				tvHomePhone = (TextView) findViewById(R.id.tvHomePhone);
				tvType = (TextView) findViewById(R.id.tvType);
				tvAuthorityLevel = (TextView) findViewById(R.id.tvAuthorityLevel);
				bntReload = (Button) findViewById(R.id.bntReload);

				tvName.setText(bundle.getString("Name"));
				tvEmail.setText(bundle.getString("Email"));
				tvApiKey.setText(bundle.getString("ApiKey"));
				tvCreateDate.setText(bundle.getString("CreateDates"));
				bntReload.setOnClickListener(btnOnClick);
			}
		}
		catch (Exception ex)
		{
			Log.e(TAG, ex.toString());
		}
	}

	private View.OnClickListener btnOnClick = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			try
			{
				JSONObject param = new JSONObject();
				param.put("Authorization", tvApiKey.getText().toString());

				WebApiAsyncTask webApiAsyncTask = new WebApiAsyncTask();
				JSONObject jsonResult = webApiAsyncTask.execute(WebApiAsyncTask.url_ViewInfo, param.toString()).get();

				if (jsonResult != null)
				{
					Log.d(TAG, jsonResult.toString());

					if (jsonResult.getBoolean("error"))
					{
						Toast.makeText(getApplicationContext(), jsonResult.getString("message"), Toast.LENGTH_SHORT).show();
					}
					else
					{
						tvTasks.setText(jsonResult.getString("tasks"));
						tvNric.setText(jsonResult.getString("nric"));
						tvMobilePhone.setText(jsonResult.getString("mobile_phone"));
						tvHomePhone.setText(jsonResult.getString("home_phone"));
						tvType.setText(jsonResult.getString("type"));
						tvAuthorityLevel.setText(jsonResult.getString("authority_level"));
					}
				}
				else
				{
					Log.e(TAG, "jsonResult is null!");
					Toast.makeText(getApplicationContext(), "Result is null!", Toast.LENGTH_SHORT).show();
				}
			}
			catch (Exception ex)
			{
				Log.e(TAG, ex.toString());
			}
		}
	};
}
